﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using HZ.MBSM.DBUtility;
public partial class Agency_TraBidPrint : System.Web.UI.Page
{
    private string _companyname_tb = "", _accountnameid = "", _accountname="", id = "";
    int n8 = 0,n9=0,n10=0,n11=0,n12=0;
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
        _AccountType.Add(HZ.MBSM.DAL.AccountType.enumSupplyCompanyCheck);
        if (HZ.MBSM.BLL.Login.CheckAssert(_AccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            if (_Account != null)
            {
                _accountname = _Account.AccountName;
               _companyname_tb= SysFun.IsSQL(Request.QueryString["companyname_tb"].ToString());
               if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
               {
                   id = SysFun.IsSQL(Request.QueryString["id"].ToString());
                   uDataBind("12");
                   IntoJilu(1);//记录打印
               }
               else
               { 
                 uDataBind("8");
                 uDataBind("9");
                 uDataBind("10");
                 uDataBind("11");
                 IntoJilu(0);//记录打印
               }
              
               ImportWord();
               
            }
        }
    }

    private void IntoJilu(int ff )//记录打印
    {
        string strUpdatU = "";
        if (ff == 12)
        {
            strUpdatU = "update SupplyPlan_Goods set Isprint=1+Isnull(Isprint,0),printTime=getdate(),PrinctName='" + _accountname + "' where isuse='1' and projectid=12  and  companyname_tb='" + _companyname_tb + "'";
        }
        else
        {
            strUpdatU = "update SupplyPlan_Goods set Isprint=1+Isnull(Isprint,0),printTime=getdate(),PrinctName='" + _accountname + "' where isuse='1' and companyname_tb='" + _companyname_tb + "'";
        }
         Db bd = new Db();
        bd.Command(strUpdatU);
        bd.Close();
    }

    /// <summary>
    /// 保存为word文件
    /// </summary>
    private void ImportWord()
    {

        Response.ContentType = "application/ms-word";
        Response.AddHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode("中标通知书" + DateTime.Now.ToShortDateString(), System.Text.Encoding.UTF8) + ".doc\"");
        StringBuilder sb = new StringBuilder();
        System.IO.StringWriter sw = new System.IO.StringWriter(sb);
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
        sb.Append("<html><body margin-top='10%' margin-bottom='10%' margin-left='15%' margin-right='15%'>");
        sb.Append("<div style='width:90%;'><p><center><h2>2007年云南省药品集中招标采购</h2></center>");
        sb.Append("<center><h2>中标通知书</h2></center></p>");
        sb.Append("</br>");
        sb.Append("</br>");
        sb.Append("<p>投标人：" + _companyname_tb + "</p>");
        sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp;贵公司在2007年云南省药品集中招标采购中中标品种：</p>");
        if (id != "")
        {
            sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp;重点监控目录：" + n12 + " 条 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
         
        }
        else
        {
            sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp;招标采购目录：" + n8 + " 条 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 血液制品目录：" + n11 + "  条</p>");
            sb.Append("<p>&nbsp;&nbsp;&nbsp;&nbsp;自行采购目录：" + n9 + " 条 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 22种降价品种目录：" + n10 + "  条</p>");
        }
        sb.Append("<p>具体明细见附表:</p>");
        sb.Append("<center>");
        if (n8 != 0)
        {
            sb.Append("<p>招标采购目录:</p>");
            this.GridView8.RenderControl(hw);
        }
        if (n9 != 0)
        {
            sb.Append("<p>自行采购目录:</p>");
            this.GridView9.RenderControl(hw);
        }
        if (n11 != 0)
        {
            sb.Append("<p>血液制品目录:</p>");
            this.GridView11.RenderControl(hw);
        }
        if (n10 != 0)
        {
            sb.Append("<p>22种降价品种目录:</p>");
            this.GridView10.RenderControl(hw);
        }
        if (n12 != 0)
        {
            sb.Append("<p>重点监控目录:</p>");
            this.GridView12.RenderControl(hw);
        }
        sb.Append("</center>");
        sb.Append("</div>");
        sb.Append("</body></html>");
        Response.Write(sb.ToString());
        Response.End();
    }


    /// <summary>
    /// 查询主配送人的配送商品
    /// </summary>
    private void uDataBind( string _projectid)
    {
        string priceTitle = "中标价";
        string fieldName = "";
        string field = "";
        switch (_projectid)
        {
            case "8":
                fieldName = "最高零售价";
                field = " ,convert( numeric(20,2), RetailPrice)  as " + fieldName;
                break;
            case "9":
                priceTitle = "最高零售价";
                break;
            case "10":
                priceTitle = "最高限价";
                fieldName = "最高零售价";
                field = " ,convert( numeric(20,2), RetailPrice)  as " + fieldName;
                break;
            case "11":
                break;
            case "12":
                priceTitle = "最高零售价";
                break;
                break;

        }

        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "SupplyPlan_Goods";      //要查询的表明
        dbPage.Key = "SupplyGoodsId";     //表的主键    
        dbPage.Where = GetSqlFilter(_projectid);     //查询条件   

        dbPage.SelectField = "goodsid as 流水号,productname as 药品通用名称,goodsname as 商品名,medicinemodel as 剂型,outlookc as 规格,factor as 转换系数,companyname_sc as 生产厂家,companyname_tb as 投标企业,convert( numeric(20,2), bidprice)  as " + priceTitle + field;//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 2000;                                                    //没有显示记录条数   
        dbPage.OrderBy = "ProductName,MedicineModel,Outlookc";//排序字段

        DataTable dt = dbPage.DataTable(60);
        switch (_projectid)
        {
            case "8":
                this.GridView8.DataSource = dt;
                this.GridView8.DataBind();
                  n8 = dt.Rows.Count;
                break;
            case "9":
                this.GridView9.DataSource = dt;
                this.GridView9.DataBind();
                n9 = dt.Rows.Count;
                break;
            case "10":
                this.GridView10.DataSource = dt;
                this.GridView10.DataBind();
                n10 = dt.Rows.Count;
                break;
            case "11":
                this.GridView11.DataSource = dt;
                this.GridView11.DataBind();
                n11 = dt.Rows.Count;
                break;
            case "12":
                this.GridView12.DataSource = dt;
                this.GridView12.DataBind();
                n12 = dt.Rows.Count;
                break;
        }
       
      
        db.Close();
    }

    /// <summary>
    /// 生成查询语句的条件
    /// </summary>
    /// <returns></returns>
    private string GetSqlFilter(string _projectid)
    {
        StringBuilder sbFilter = new StringBuilder("  isuse='1' and projectid=" + _projectid + " and companyname_tb='" + _companyname_tb + "'");
     
        return sbFilter.ToString();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
    } 
}
